package com.prawn.ift.ctrlequipment.service.impl;

import com.prawn.ift.ctrlequipment.dao.CtrlequipmentTypeStateDao;
import com.prawn.ift.ctrlequipment.pojo.CtrlequipmentType;
import com.prawn.ift.ctrlequipment.pojo.CtrlequipmentTypeState;
import com.prawn.ift.ctrlequipment.service.CtrlequipmentTypeStateService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import util.IdWorker;
import util.NullAttributeUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 服务层
 * 
 * @author Administrator
 *
 */
@Service
public class CtrlequipmentTypeStateServiceImpl implements CtrlequipmentTypeStateService {

	@Autowired
	private CtrlequipmentTypeStateDao ctrlequipmentTypeStateDao;
	
	@Autowired
	private IdWorker idWorker;

	/**
	 * 查询全部列表
	 * @return
	 */
	public List<CtrlequipmentTypeState> findAll() {
		return ctrlequipmentTypeStateDao.findAll();
	}

	
	/**
	 * 条件查询+分页
	 * @param whereMap
	 * @param page
	 * @param size
	 * @return
	 */
	public Page<CtrlequipmentTypeState> findSearch(Map whereMap, int page, int size) {
		Specification<CtrlequipmentTypeState> specification = createSpecification(whereMap);
		PageRequest pageRequest =  PageRequest.of(page-1, size);
		return ctrlequipmentTypeStateDao.findAll(specification, pageRequest);
	}

	
	/**
	 * 条件查询
	 * @param whereMap
	 * @return
	 */
	public List<CtrlequipmentTypeState> findSearch(Map whereMap) {
		Specification<CtrlequipmentTypeState> specification = createSpecification(whereMap);
		return ctrlequipmentTypeStateDao.findAll(specification);
	}

	/**
	 * 根据ID查询实体
	 * @param id
	 * @return
	 */
	public CtrlequipmentTypeState findById(String id) {
		return ctrlequipmentTypeStateDao.findById(id).get();
	}

	/**
	 * 增加
	 * @param ctrlequipmentTypeState
	 */
	public void add(CtrlequipmentTypeState ctrlequipmentTypeState) {
		ctrlequipmentTypeState.setId( idWorker.nextId()+"" );
		ctrlequipmentTypeStateDao.save(ctrlequipmentTypeState);
	}

	/**
	 * 修改
	 * @param ctrlequipmentTypeState
	 */
	public void update(CtrlequipmentTypeState ctrlequipmentTypeState) {


		CtrlequipmentTypeState oldCtrlequipmentTypeState =ctrlequipmentTypeStateDao.findById(ctrlequipmentTypeState.getId()).get();
		BeanUtils.copyProperties(ctrlequipmentTypeState,oldCtrlequipmentTypeState, NullAttributeUtils.getNullPropertyNames(ctrlequipmentTypeState));



		ctrlequipmentTypeStateDao.save(oldCtrlequipmentTypeState);
	}

	/**
	 * 删除
	 * @param id
	 */
	public void deleteById(String id) {
		ctrlequipmentTypeStateDao.deleteById(id);
	}

	/**
	 * 动态条件构建
	 * @param searchMap
	 * @return
	 */
	private Specification<CtrlequipmentTypeState> createSpecification(Map searchMap) {

		return new Specification<CtrlequipmentTypeState>() {

			@Override
			public Predicate toPredicate(Root<CtrlequipmentTypeState> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicateList = new ArrayList<Predicate>();
                // 编号
                if (searchMap.get("id")!=null && !"".equals(searchMap.get("id"))) {
                	predicateList.add(cb.like(root.get("id").as(String.class), "%"+(String)searchMap.get("id")+"%"));
                }
                // 真实通道id
                if (searchMap.get("rel_id")!=null && !"".equals(searchMap.get("rel_id"))) {
                	predicateList.add(cb.like(root.get("rel_id").as(String.class), "%"+(String)searchMap.get("rel_id")+"%"));
                }
                // 状态按钮
                if (searchMap.get("state_url")!=null && !"".equals(searchMap.get("state_url"))) {
                	predicateList.add(cb.like(root.get("state_url").as(String.class), "%"+(String)searchMap.get("state_url")+"%"));
                }
                // 状态名称
                if (searchMap.get("state_name")!=null && !"".equals(searchMap.get("state_name"))) {
                	predicateList.add(cb.like(root.get("state_name").as(String.class), "%"+(String)searchMap.get("state_name")+"%"));
                }
                // 状态类型
                if (searchMap.get("state_type")!=null && !"".equals(searchMap.get("state_type"))) {
                	predicateList.add(cb.like(root.get("state_type").as(String.class), "%"+(String)searchMap.get("state_type")+"%"));
                }
                // 范围上限
                if (searchMap.get("range_cap")!=null && !"".equals(searchMap.get("range_cap"))) {
                	predicateList.add(cb.like(root.get("range_cap").as(String.class), "%"+(String)searchMap.get("range_cap")+"%"));
                }
                // 范围下限
                if (searchMap.get("range_lower")!=null && !"".equals(searchMap.get("range_lower"))) {
                	predicateList.add(cb.like(root.get("range_lower").as(String.class), "%"+(String)searchMap.get("range_lower")+"%"));
                }
                // 
                if (searchMap.get("remarks")!=null && !"".equals(searchMap.get("remarks"))) {
                	predicateList.add(cb.like(root.get("remarks").as(String.class), "%"+(String)searchMap.get("remarks")+"%"));
                }
                // 
                if (searchMap.get("create_by")!=null && !"".equals(searchMap.get("create_by"))) {
                	predicateList.add(cb.like(root.get("create_by").as(String.class), "%"+(String)searchMap.get("create_by")+"%"));
                }
                // 
                if (searchMap.get("update_by")!=null && !"".equals(searchMap.get("update_by"))) {
                	predicateList.add(cb.like(root.get("update_by").as(String.class), "%"+(String)searchMap.get("update_by")+"%"));
                }
                // 
                if (searchMap.get("del_flag")!=null && !"".equals(searchMap.get("del_flag"))) {
                	predicateList.add(cb.like(root.get("del_flag").as(String.class), "%"+(String)searchMap.get("del_flag")+"%"));
                }
				
				return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));

			}
		};

	}

}
